package kmn.udpservice.activity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import kmn.udpservice.*;

import kmn.udpservice.data.AuthentificationData;
import kmn.udpservice.service.*;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{
	boolean bound = false;
	UDPservice service;
	
	private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName cpn,
                IBinder serv) {
        	UDPservice.UDPserviceBinder binder = (UDPservice.UDPserviceBinder) serv;
            service = binder.getService();
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName cpn) {
            bound = false;
        }
    };

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = new Intent(this, UDPservice.class);
        bindService(intent, connection, this.BIND_AUTO_CREATE);

		//startService(intent);
		Button button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) 
			{
				//EditText edit = (EditText)findViewById(R.id.editText1);
				Intent intent = new Intent(getApplicationContext(), UDPservice.class);
				startService(intent);
				
				
				Log.d("just a test", "before onCreate");
				ArrayList<String> list = new ArrayList<String>();
				list.add("Moi"); list.add("Toi"); list.add("Vous");
				//intent.putStringArrayListExtra("list", list);
				
				
				AuthentificationData data = new AuthentificationData("clementDaboss", "1304xxxx");
				intent.putExtra("data", data);
				
				
				service.addListener(new IUDPserviceListener(){
				    public void dataChanged(Object o) {
				    	AuthentificationData data = (AuthentificationData)o;
				    	if (data != null)
				    	{
				    		if (data.getResult() == 1)
				    		{
				    			Toast.makeText(getApplicationContext(), "Authentification valide !", Toast.LENGTH_LONG).show();
				    		}
				    		else
				    		{
				    			Toast.makeText(getApplicationContext(), "Authentification invalide !", Toast.LENGTH_LONG).show();
				    		}
				    	}
				    	else
				    	{
				    		Toast.makeText(getApplicationContext(), "Authentification invalide !", Toast.LENGTH_LONG).show();
				    	}	
				    } 
				});
				
			}
		});
	}
	
	
}
